<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('demand.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('demand.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('demand.demand_type')" prop="demand_type">
                    <el-select
                      v-model="dataForm.demand_type"
                      :placeholder="i18n('common.please_select') + i18n('demand.demand_type')" clearable>
                      <el-option v-for="(v, k) in demandTypeList" :label="v.text" :key="k" :value="v.value"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('demand.client_id')" prop="client_id">
                    <el-select
                      v-model="dataForm.client_id"
                      :placeholder="i18n('common.please_select') + i18n('demand.client_id')"  @change="loadContactList" :disabled="status" clearable>
                      <el-option v-for="(v, k) in clientList" :label="v.name" :key="k" :value="v.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('demand.contact_id')" prop="contact_id">
                    <el-select
                      v-model="dataForm.contact_id"
                      :placeholder="i18n('common.please_select') + i18n('demand.contact_id')" clearable>
                      <el-option v-for="(v, k) in contactList" :label="v.name" :key="k" :value="v.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('demand.demand_time')" prop="demand_time">
                    <el-date-picker v-model="dataForm.demand_time" type="month" :placeholder="i18n('common.please_input') + i18n('demand.demand_time')" clearable/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="$t('demand.money')" prop="money">
                    <el-input-number v-model="dataForm.money" :min="0"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('common.sort')" prop="sort">
                    <el-input-number v-model="dataForm.sort" :min="0">
                    </el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="24">
                  <el-form-item :label="i18n('demand.description')" prop="description">
                    <el-input
                      type="textarea"
                      v-model="dataForm.description"
                      :rows="5"
                      show-word-limit
                      maxlength="500"
                      :placeholder="i18n('common.please_input') + i18n('demand.description')">
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('common.is_recommend')" prop="is_recommend">
                    <el-switch
                      v-model="dataForm.is_recommend"
                      active-value="1"
                      :active-text="i18n('common.yes')"
                      inactive-value="2"
                      :inactive-text="i18n('common.no')">
                    </el-switch>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('common.is_hidden')" prop="is_hidden">
                    <el-switch
                      v-model="dataForm.is_hidden"
                      active-value="1"
                      :active-text="i18n('common.yes')"
                      inactive-value="2"
                      :inactive-text="i18n('common.no')">
                    </el-switch>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('common.status')" prop="status">
                    <el-switch
                      v-model="dataForm.status"
                      active-value="1"
                      :active-text="i18n('common.enable')"
                      inactive-value="2"
                      :inactive-text="i18n('common.disable')">
                    </el-switch>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:business:demand:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="demand-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/demand')

  const status = ref(false)
  const clientList = ref([])
  const contactList = ref([])
  const demandTypeList = ref([])

  const dataForm = reactive({
    id: 0,
    user_id: 0,
    client_id: '',
    contact_id: '',
    demand_type: '',
    demand_time: '',
    money: 0,
    description: '',
    sort: 0,
    is_recommend: '2',
    is_hidden: '2',
    status: '1',
  })

  const dataRule = reactive({
    demand_type: [{
      required: true,
      message: i18n('demand.rules.demand_type.require'),
      trigger: 'blur',
    }],
    client_id: [{
      required: true,
      message: i18n('demand.rules.client_id.require'),
      trigger: 'blur',
    }],
    contact_id: [{
      required: true,
      message: i18n('demand.rules.contact_id.require'),
      trigger: 'blur',
    }],
    demand_time: [{
      required: true,
      message: i18n('demand.rules.demand_time.require'),
      trigger: 'blur',
    }],
    money: [{
      required: true,
      message: i18n('demand.rules.money.require'),
      trigger: 'blur',
    }],
    description: [{
      required: true,
      message: i18n('demand.rules.description.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.user_id = data.data.user_id
            dataForm.client_id = data.data.client_id
            dataForm.contact_id = data.data.contact_id
            dataForm.demand_type = data.data.demand_type.value + ''
            dataForm.demand_time = data.data.demand_time
            dataForm.money = data.data.money
            dataForm.description = data.data.description
            dataForm.sort = data.data.sort
            dataForm.is_recommend = data.data.is_recommend.value + ''
            dataForm.is_hidden = data.data.is_hidden.value + ''
            dataForm.status = data.data.status.value + ''
          }
        }).then(() => {
          if (dataForm.client_id) {
            $http({
              url: $http.adornUrl(`/business/contact/select`),
              method: 'get',
              params: $http.adornParams({
                client_id: dataForm.client_id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                contactList.value = data.data
              }
            })
          }
        })
      }
    })
  }


  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            user_id: dataForm.user_id,
            client_id: dataForm.client_id,
            contact_id: dataForm.contact_id,
            demand_type: dataForm.demand_type,
            demand_time: dataForm.demand_time,
            money: dataForm.money,
            description: dataForm.description,
            sort: dataForm.sort,
            is_recommend: dataForm.is_recommend,
            is_hidden: dataForm.is_hidden,
            status: dataForm.status,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }


  const loadClientList = () => {
    $http({
      url: $http.adornUrl('/business/client/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        clientList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }


  const loadContactList = (client_id) => {
    $http({
      url: $http.adornUrl('/business/contact/select'),
      method: 'get',
      params: $http.adornParams({
        client_id: client_id
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        contactList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }


  const loadDemandTypeList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'demand_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        demandTypeList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  if(query.client_id) {
    dataForm.client_id = parseInt(query.client_id)
  }

  onMounted(() => {
    init(formRef)

    loadClientList()
    loadDemandTypeList()

    if(query.client_id) {
      status.value = true
      loadContactList(query.client_id)
    }
  })
</script>
